<?php

namespace app\service;

class SystemMenuDataPower
{
    /**
     * 登录人是否存在人才公海，注册企业公海，合作伙伴公海权限
     * @param $user_login_id //登录人uid
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOpenSeaPower($user_login_id)
    {
        //初始化数据
        $data = [
            'rcgh' => false,//人才公海
            'zcqygh' => false,//注册企业公海
            'hzhbgh' => false//合作伙伴公海
        ];

        $dataPower = new \app\model\SystemMenuDataPower();
        $user = new \app\model\User();
        //如果用户不存在返回默认数据
        $udata = $user->getIdUserData($user_login_id);
        if ($udata['username'] == 'admin'){
            return [
                'rcgh' => true,//人才公海
                'zcqygh' => true,//注册企业公海
                'hzhbgh' => true//合作伙伴公海
            ];
        }
        if(!$user_login_id || !$udata) return $data;
        //获取登录人角色权限
        $roles_id = is_array($udata['level_id']) ? $udata['level_id']['id'] : $udata['level_id'];
        $roles_power = $dataPower->getPositionDataDetails($roles_id);
        //如果无该角色或者该角色无权限返回默认数据
        if(!$roles_power || ($roles_power && !$roles_power['menu_alias_id'])) return $data;
        //如果存在权限将权限设置为true
        $menu_alias_id_arr = explode(',',$roles_power['menu_alias_id']);
        $data_keys = array_keys($data);
        foreach($data_keys as $value){
            if(in_array($value,$menu_alias_id_arr)){
                $data[$value] = true;
            }
        }
        return $data;
    }
}